package com.qihetimes.earthangel.controller;

import com.qihetimes.earthangel.common.ResponseResult;
import com.qihetimes.earthangel.common.annotation.NoLogin;
import com.qihetimes.earthangel.model.Feedback;
import com.qihetimes.earthangel.model.User;
import com.qihetimes.earthangel.model.bo.UserBO;
import com.qihetimes.earthangel.model.bo.UserSearchBO;
import com.qihetimes.earthangel.model.bo.UserWxBO;
import com.qihetimes.earthangel.model.vo.PageVO;
import com.qihetimes.earthangel.model.vo.UserCenterVO;
import com.qihetimes.earthangel.model.vo.UserVO;
import com.qihetimes.earthangel.service.IFeedbackService;
import com.qihetimes.earthangel.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户控制器
 *
 * @author ：f4cklangzi@gmail.com
 * @date ：Created in 2021/12/4 17:41
 */
@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {
    
    @Resource
    private IUserService userService;
    
    @Resource
    private IFeedbackService feedbackService;
    
    @NoLogin
    @ApiOperation(value = "微信手机号注册/登录")
    @PostMapping("/wxLogin")
    public ResponseResult<String> wxLogin(@RequestBody UserWxBO param) {
        String token = userService.wxLogin(param);
        return ResponseResult.success(token);
    }
    
    @NoLogin
    @ApiOperation(value = "jsCode登录")
    @PostMapping("/jsCodeLogin")
    public ResponseResult<String> jsCodeLogin(@RequestBody UserWxBO param) {
        String token = userService.jsCodeLogin(param);
        return ResponseResult.success(token);
    }
    
    @ApiOperation(value = "微信手机号注册/登录后绑定openid用于")
    @PostMapping("/bindOpenId")
    public ResponseResult<String> bindOpenId(@RequestBody UserWxBO param) {
        userService.bindOpenId(param);
        return ResponseResult.success();
    }
    
    @NoLogin
    @ApiOperation(value = "手机验证码注册/登录")
    @GetMapping("/codeLogin")
    public ResponseResult<String> codeLogin(@RequestParam String phone, @RequestParam Integer code) {
        String token = userService.codeLogin(phone, code);
        return ResponseResult.success(token);
    }
    
    @ApiOperation(value = "获取个人中心信息")
    @GetMapping("/userCenter")
    public ResponseResult<UserCenterVO> userCenter() {
        UserCenterVO info = userService.userCenter();
        return ResponseResult.success(info);
    }
    
    @ApiOperation(value = "更新用户信息")
    @PutMapping("/update")
    public ResponseResult<User> update(@RequestBody UserBO param) {
        User user = userService.update(param);
        return ResponseResult.success(user);
    }
    
    @ApiOperation(value = "搜索用户")
    @PostMapping("/search")
    public ResponseResult<PageVO<UserVO>> search(@RequestBody UserSearchBO param) {
        PageVO<UserVO> info = userService.search(param);
        return ResponseResult.success(info);
    }
    
    @ApiOperation(value = "删除账户")
    @DeleteMapping("/delete")
    public ResponseResult<String> delete() {
        userService.delete();
        return ResponseResult.success();
    }
    
    @ApiOperation(value = "意见反馈")
    @PostMapping("/feedback")
    public ResponseResult<String> feedback(@RequestBody Feedback param) {
        feedbackService.add(param);
        return ResponseResult.success();
    }
    
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public ResponseResult<String> logout() {
        userService.logout();
        return ResponseResult.success();
    }
    
    @ApiOperation(value = "根据IP地址获取所属城市")
    @GetMapping("/ipToCity")
    public ResponseResult<String> ipToCity(String ip) {
        String city = userService.ipToCity(ip);
        return ResponseResult.success(city);
    }
}
